package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(QueryObject qo, Model model){
        model.addAttribute("pageInfo",roleService.selectList(qo));
        return "role/list";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){
        model.addAttribute("allPermissions",permissionService.selectAll());
        if(id!=null){
            model.addAttribute("selfPermissions",permissionService.selectByRoleId(id));
            model.addAttribute("role",roleService.get(id));
        }
        return "role/input";
    }

    @RequiredPermission(name = "角色新增/修改", expression = "role.saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids){
        if (role.getId()==null) {
            roleService.save(role,ids);
        }else{
            roleService.update(role,ids);
        }
        return "redirect:/role/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        roleService.delete(id);

        return "redirect:/role/list";
    }

}
